import moment from 'moment'

function getDataList (type, deviceList, params) {
  let dataList = {
    'code': '1200',
    'message': '操作成功',
    'data': [],
    'success': true
  }
  const XLabel = getAxisList(params)
  console.log(XLabel)
  deviceList.forEach(item => {
    let device = {
      deviceId: item.value,
      deviceName: item.label,
      assetDevice: item.assetDevice,
      deviceInfo: []
    }

    XLabel.forEach(item => {
      let deviceInfo = {
        date: item,
        planPowerOnTime: (parseInt(Math.random() * 7600) + 50000) * type,
        academicBeat: (parseInt(Math.random() * 7600) + 50000) * type,
        actualProductCount: parseInt(Math.random() * 600)
      }
      deviceInfo['passRate'] = ((Math.random() * 40 + 60) / 100).toFixed(2)
      deviceInfo['timeRate'] = ((Math.random() * 70 + 60) / 100).toFixed(2)
      deviceInfo['performanceRate'] = ((Math.random() * 70 + 60) / 100).toFixed(2)
      deviceInfo['activation'] = (deviceInfo.passRate * deviceInfo.timeRate * deviceInfo.performanceRate).toFixed(5)
      deviceInfo['actualMachTime'] = parseInt(deviceInfo.planPowerOnTime * deviceInfo.timeRate)
      deviceInfo['actualRunTime'] = parseInt(deviceInfo.academicBeat * deviceInfo.performanceRate)
      deviceInfo['badCount'] = parseInt(deviceInfo.actualProductCount * deviceInfo.passRate)
      device.deviceInfo.push(deviceInfo)
    })
    dataList.data.push(device)
  })
  console.log(type, dataList)
  return dataList
}
function getAxisList (params) {
  let XLabel = []
  let rangeDiff = null
  if (params.dateType === 'date') {
    const startDate = moment(params.dateRange[0])
    const endDate = moment(params.dateRange[1])
    rangeDiff = endDate.diff(startDate, 'days')
    for (let i = 0; i <= rangeDiff; i++) {
      const firstDate = moment(params.dateRange[0])
      const date = firstDate.add(i, 'd').format('YYYY-MM-DD')
      XLabel.push(date)
    }
  } else if (params.dateType === 'week') {
    const startDate = moment(params.weekRange[0])
    const endDate = moment(params.weekRange[1])
    rangeDiff = endDate.diff(startDate, 'week')

    for (let i = 0; i <= rangeDiff; i++) {
      const firstDate = moment(params.weekRange[0])
      const date = firstDate.add(i, 'week').format('YYYY-MM-DD')
      XLabel.push(date)
    }
  } else if (params.dateType === 'month') {
    const startDate = moment(params.monthRange[0])
    const endDate = moment(params.monthRange[1])
    rangeDiff = endDate.diff(startDate, 'month')
    for (let i = 0; i <= rangeDiff; i++) {
      const firstDate = moment(params.monthRange[0])
      const date = firstDate.add(i, 'month').format('YYYY-MM-DD')
      XLabel.push(date)
    }
  }
  return XLabel
}
export default {
  getDataList
}
